import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/phone',
    component: () => import('@/views/login/phone'),
    hidden: true
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  {
    path: '/example',
    component: Layout,
    name: 'Example',
    redirect: '/example/User',
    meta: { title: '员工管理', icon: 'example' },
    children: [
      {
        path: 'User',
        name: 'User',
        component: () => import('@/views/user/index'),
        meta: { title: '员工列表', icon: 'user' }
      }
    ]
  },
  {
    path: '/Order',
    component: Layout,
    name: 'Order',
    redirect: '/Order/orderList',
    meta: { title: '转账工具管理', icon: 'example' },
    children: [
      {
        path: 'orderIndex',
        name: 'orderIndex',
        component: () => import('@/views/order/index'),
        meta: { title: '最新转账工具', icon: 'table' }
      },
      {
        path: 'orderList',
        name: 'orderList',
        component: () => import('@/views/order/list'),
        meta: { title: '交易记录查询', icon: 'table' }
      }
    ]
  }
];

// 解决重复点击导航路由报错
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
}

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
});

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
